package com.cloud.code.并发包;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * 描述:
 * BlockingQueue
 *
 * @author 54485
 * @create 2018-09-18 11:29
 */
public class BlockingQueueTest {

    public static void main(String[] args) {

        BlockingDeque blockingDeque = new LinkedBlockingDeque(3);

        new Thread(new Runnable() {
            int i = 0;

            @Override
            public void run() {
                while (true) {
                    try {
                        blockingDeque.offer(i,2,TimeUnit.SECONDS);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("add"+i);
                    i++;


                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            int i = 0;

            @Override
            public void run() {
                while (true) {
                    try {
                        Object poll = blockingDeque.poll(1, TimeUnit.SECONDS);
                        System.out.println("get"+poll);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
